<?php
// $Id$

/**
 * Controller_Commonarticles 控制器
 */
class Controller_Commonarticle extends Controller_Abstract
{
    function actionIndex()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/index'));
        }
        //$current_user_id = $this->_app->currentUserObject()->user_id;
        // 分页查询
        $page = intval($this->_context->page);
        if ($page < 1) $page = 1;
        // 构造查询对象
        $select = CommonArticle::find()->order('article_id DESC')->all();
        $select->limitPage($page, 10);

        // 将分页信息和查询到的数据传递到视图
        $this->_view['pagination'] = $select->getPagination();
        $this->_view['articles'] = $select->getAll();

        $this->_view['num'] = $select->getCount();
        $this->_viewname = 'index';
    }
    
    function actionPreviewArticle()
    {
        if(null == $this->_app->currentUser())
        {
            return $this->_redirect(url('default/Index'));
        }
        $current_user = $this->_app->currentUserObject();
        if(1 != $current_user->permission_id)
        {
            return $this->_redirect(url('default/index'));
        }
        $article_id = $this->_context->article_id;
        $article = CommonArticle::find('article_id = ?', $article_id)->getOne();
        $url = Q::ini('appini/web_site_ip').$article->article_url;

        $this->_view['article_url'] = $url;
        $this->_view['article'] = $article;
        $this->_viewname = 'previewarticle';
    }

    function actionRemove()
    {
        if(isset($_POST['checkboxid']))
        {
            $id=$_POST['checkboxid'];

            foreach ($id as $value)
            {
                CommonArticle::meta()->destroyWhere('article_id = ?', $value);
            }
        }
        return $this->_redirectMessage(
            '文章删除',
            '文章已经被成功删除',
            url('CommonArticle/index'));
    }

    //编辑文章，原来生成的html文件需要删掉
    function dumpFileContent2StaticHtml($article_id, $article_name, $article_content)
    {
        $file_dir =  $this->_app->config('ROOT_DIR').'/static/common_html/' . date('Ym');
        $url_path = 'static/common_html/' . date('Ym');
        if(!file_exists($file_dir))
        {
            mkdir($file_dir);
        }
        $file_name = $file_dir.'/'.$article_id.'.html';
        $html_file_fp = fopen($file_name, 'w');
        $header_str='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8" />';
        $html_header = sprintf($header_str, $article_name);
        $html_tail = "\n</body>\n</html>";
        //dump($html_header, "header");
        //dump($article_content, "content");
        //dump($html_tail);
        //dump($html_header.$article_content.$html_tail);
        fwrite($html_file_fp, $html_header.$article_content.$html_tail);
        fclose($html_file_fp);

        return $url_path.'/'.$article_id.'.html';
    }

    function actionEdit()
    {
        $current_user = $this->_app->currentUserObject();
        $columns_array = Column::find()->getAll();

        $article_id = $this->_context->article_id;
        $article = CommonArticle::find('article_id = ?', $article_id)->query();
        //dump($article);
        if (!$_POST)
        {
            $this->_view['article'] = $article;
            $this->_view['article_id'] = $article_id;
            $this->_view['columns'] = $columns_array;
            $this->_view['article_name'] = $article->article_name;
            $this->_view['article_content'] = $article->article_content;
            $this->_view['column_id'] = $article->channel_id;
            $this->_view['article_summary'] = $article->article_summary;
            $this->_viewname = 'edit';
        }
        else
        {
            $article->article_name = $_POST['article_topic'];
            $article->article_content = $_POST['article_content'];
            //$article->channel_id = $_POST['article_column'];
            if(isset($_POST['summary']))
            {
                $article->article_summary = $_POST['summary'];
            }
            $article->article_latest_update_timestamp = time();
            //unlink();
            $old_path = $this->_context->baseDir().$article->article_url;
            //unlink($old_path);
            $file_name = $this->dumpFileContent2StaticHtml($article->article_id, $_POST['article_topic'], $_POST['article_content']);
            $default_logo = $this->_context->baseDir().'static/const_img/default.jpg';
            $uploadpic = $_POST['uploadpic'];
            //$article->article_icon = $default_logo;
            $content_image = $_POST['content_image'];

            if(isset($_POST['content_image']) && strlen($content_image) > 0)
            {
                $article->article_icon =  $content_image;
            }
            if(isset($_POST['uploadpic']) && strlen($uploadpic) > 0)
            {
                $web_site = Q::ini('appini/web_site_ip');
                if(strpos($uploadpic, $web_site) === false)
                {
                    $article->article_icon = $web_site . $uploadpic;
                }
                else
                {
                    $article->article_icon = $uploadpic;
                }

            }
            $article->article_url = $file_name;

            $article->save();
            return $this->_redirectMessage(
                '编辑文章',
                '文章编辑成功',
                url('CommonArticle/PreviewArticle', array("article_id"=>$article->id())));
        }
    }


    function actionAdd()
    {
        // 构造表单对象
        $current_user = $this->_app->currentUserObject();
        //dump($enterprise);

        $columns_array = Channel::find()->getAll();
        //dump($columns_array);

        //dump($this->_context->isPOST());
        if ($this->_context->isPOST())
        {
            //dump($this->_context->isPOST());
            // 是 POST 提交，并且表单验证通过
            try
            {
                // 创建 article 对象并保存
                $article = new CommonArticle();
                $article->channel_id = 3;//$_POST['article_column'];
                $article->article_name = $_POST['article_topic'];
                $article->article_content = $_POST['article_content'];
                $article->create_user_id = $current_user->user_id;
                if(isset($_POST['summary']))
                {
                    $article->article_summary = $_POST['summary'];
                }
                $article->audit_user_id = $current_user->user_id;
                $create_time = time();
                $article->article_audit_timestamp=$create_time;
                $article->article_create_timestamp = $create_time;
                $article->article_latest_update_timestamp = $create_time;
                $article->article_url = "http://localhost";//临时地址
                $article->save();				
                $file_name = $this->dumpFileContent2StaticHtml($article->id(), 
                        $article->article_name, $article->article_content);
                $article->article_url = $file_name;
                $web_site=Q::ini('appini/web_site_ip');
                $content_image = $_POST['content_image'];
                $uploadpic = "";
                if(isset($_POST['uploadpic']))
                {
                    $uploadpic = $_POST['uploadpic'];
                }
                $default_logo = $web_site . $this->_context->baseDir().'static/const_img/telecom_logo.png';
                $article->article_icon = $default_logo;
                if($content_image)
                {
                    $article->article_icon =  $content_image;
                }
                if($uploadpic)
                {
                    $article->article_icon = $web_site . $uploadpic;
                }

                $article->save();
                return $this->_redirectMessage(
                    '发布文章',
                    '文章发布成功',
                    url('CommonArticle/PreviewArticle', array("article_id"=>$article->id())));
            }
            catch (ValidateFailedException $ex)
            {
                return $this->_redirectMessage(
                    '发布文章出错',
                    '发布文章出错',
                    url('CommonArticle/index'));
            }
        }

        // 将表单对象传递给视图
        $this->_view['columns'] = $columns_array;
    }
}


